import assert from 'assert';
import { Context, Next } from 'koa';

export default async function(ctx: Context, next: Next) {
    try {
        await next();
        if (typeof ctx.body !== 'undefined') {
            ctx.status = 200;
            ctx.body = {
                data: ctx.body,
                code: 200,
            };
        }
    } catch (error) {
        if (error instanceof assert.AssertionError) {
            ctx.body = {
                data: ctx.body,
                code: 400,
            };
        }
    }
}
